import {
  Column,
  Entity,
  JoinColumn,
  ManyToMany,
  ManyToOne,
  OneToMany,
  OneToOne,
  Point,
  Relation,
} from 'typeorm';

import { CommonEntity } from '~/common/entity/common.entity';

import { AccessTokenEntity } from '~/modules/auth/entities/access-token.entity';

import { DeptEntity } from '~/modules/system/dept/dept.entity';
import { RoleEntity } from '~/modules/system/role/role.entity';
import { UserWechatEntity } from './user-wechat/user-wechat.entity';
import { TenantEntity } from '~/modules/tenant/tenant.entity';
import { UserAdminEntity } from './user-admin/user-admin.entity';
import { MissionUserEntity } from '../mission/mission-user.entity';
import { ArticleContentCollectEntity } from '../article/article-content/article-content-collect/aritcle-content-collect.entity';
import { ArticleContentLikeEntity } from '../article/article-content/article-content-like/article-content-like.entity';
import { PointsEntity } from '../points/points.entity';
import { PointsLogEntity } from '../points/points-log/points-log.entity';
import { PointsGoodsExchangeEntity } from '../points/points-goods/points-goods-exchange/points-goods-exchange.entity';
import { DictItemEntity } from '../system/dict-item/dict-item.entity';
import { ArticleContentUserReviewEntity } from '../article/article-content/article-content-user-review/article-content-user-review.entity';
import { ArticleContentShareEntity } from '../article/article-content/article-content-share/article-content-share.entity';

@Entity({ name: 'sys_user' })
export class UserEntity extends CommonEntity {
  @Column({ nullable: true })
  remark: string;

  @Column({ type: 'tinyint', nullable: true, default: 1 })
  status: number;

  @ManyToOne(() => DeptEntity, (dept) => dept.users)
  @JoinColumn({ name: 'dept_id' })
  dept: Relation<DeptEntity>;

  @OneToMany(() => AccessTokenEntity, (accessToken) => accessToken.user, {
    cascade: true,
  })
  accessTokens: Relation<AccessTokenEntity[]>;

  @OneToOne(() => UserWechatEntity, (userWechat) => userWechat.user, {
    cascade: ['insert', 'update'],
    nullable: true,
  })
  @JoinColumn({ name: 'wechat_id' }) // 外键关联 `user_wechat` 表的 `wechat_id`
  wechat: UserWechatEntity;

  @OneToOne(() => UserAdminEntity, (userAdmin) => userAdmin.user, {
    cascade: ['insert', 'update'],
    nullable: true,
  })
  @JoinColumn({ name: 'admin_id' }) // 外键关联 `user_admin` 表的 `admin_id`
  admin: UserAdminEntity;

  @ManyToOne(() => RoleEntity, (role) => role.users)
  @JoinColumn({ name: 'role_id' })
  role: Relation<RoleEntity>;

  @ManyToOne(() => TenantEntity, (tenant) => tenant.users)
  @JoinColumn({ name: 'tenant_id' })
  tenant: TenantEntity;

  // 用户与任务的多对多关系，通过 MissionUserEntity 连接表
  @OneToMany(() => MissionUserEntity, (missionUser) => missionUser.user)
  missionUsers: MissionUserEntity[];

  @OneToMany(() => ArticleContentCollectEntity, (contentCollect) => contentCollect.user)
  contentCollects: ArticleContentCollectEntity[];

  @OneToMany(() => ArticleContentLikeEntity, (contentCollect) => contentCollect.user)
  contentLikes: ArticleContentLikeEntity[];

	@OneToMany(() => ArticleContentShareEntity, (contentShare) => contentShare.user)
	contentShares: ArticleContentShareEntity[];

  @OneToOne(() => PointsEntity, (points) => points.user)
  points: PointsEntity;

  @OneToMany(() => PointsLogEntity, (pointsLog) => pointsLog.user)
  pointsLog: PointsLogEntity[];

  @OneToMany(() => PointsGoodsExchangeEntity, (exchanges) => exchanges.goods)
  pointsGoodsExchanges: PointsGoodsExchangeEntity[];

  @ManyToMany(() => DictItemEntity, (dictItem) => dictItem.users, { cascade: true })
  labels: DictItemEntity[];

	@OneToMany(() => ArticleContentUserReviewEntity, (user) => user.user)
	articleContentUserReview: ArticleContentUserReviewEntity[];
}
